<?xml version="1.0" encoding="utf-8"?>
<!--
                                                                                     
 h       t     t                ::       /     /                     t             / 
 h       t     t                ::      //    //                     t            // 
 h     ttttt ttttt ppppp sssss         //    //  y   y       sssss ttttt         //  
 hhhh    t     t   p   p s            //    //   y   y       s       t          //   
 h  hh   t     t   ppppp sssss       //    //    yyyyy       sssss   t         //    
 h   h   t     t   p         s  ::   /     /         y  ..       s   t    ..   /     
 h   h   t     t   p     sssss  ::   /     /     yyyyy  ..   sssss   t    ..   /     
                                                                                     
	<https://y.st./>
	Copyright © 2017 Alex Yst <mailto:copyright@y.st>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <https://www.gnu.org./licenses/>.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="https://y.st./en/coursework/CS1101/bool.py.xhtml" />
		<title>bool.py &lt;https://y.st./en/coursework/CS1101/bool.py.xhtml&gt;</title>
		<link rel="icon" type="image/png" href="/link/CC_BY-SA_4.0/y.st./icon.png" />
		<link rel="stylesheet" type="text/css" href="/link/basic.css" />
		<link rel="stylesheet" type="text/css" href="/link/site-specific.css" />
		<script type="text/javascript" src="/script/javascript.js" />
		<meta name="viewport" content="width=device-width" />
	</head>
	<body>
		<nav>
			<p>
				<a href="/en/">Home</a> |
				<a href="/en/a/about.xhtml">About</a> |
				<a href="/en/a/contact.xhtml">Contact</a> |
				<a href="/a/canary.txt">Canary</a> |
				<a href="/en/URI_research/"><abbr title="Uniform Resource Identifier">URI</abbr> research</a> |
				<a href="/en/opinion/">Opinions</a> |
				<a href="/en/coursework/">Coursework</a> |
				<a href="/en/law/">Law</a> |
				<a href="/en/a/links.xhtml">Links</a> |
				<a href="/en/coursework/CS1101/bool.py.xhtml.asc">{this page}.asc</a>
			</p>
			<hr/>
			<p>
				<a href="/en/coursework/BUS1101/" title="Principles of Business Management">BUS 1101</a> |
				<span class="hyperlink_unavailable" title="Basic Accounting">BUS 1102</span> |
				<span class="hyperlink_unavailable" title="Principles of Marketing">BUS 2201</span> |
				<span class="hyperlink_unavailable" title="Multinational Management">BUS 2207</span> |
				<span class="hyperlink_unavailable" title="Business and Society">BUS 3306</span> |
				<a href="/en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a> |
				<a href="/en/coursework/CS1102/" title="Programming 1">CS 1102</a> |
				<a href="/en/coursework/CS1103/" title="Programming 2">CS 1103</a> |
				<span class="hyperlink_unavailable" title="Computer Systems">CS 1104</span> |
				<a href="/en/coursework/CS2203/" title="Databases 1">CS 2203</a> |
				<span class="hyperlink_unavailable" title="Communications and Networking">CS 2204</span> |
				<a href="/en/coursework/CS2205/" title="Web Programming 1">CS 2205</a> |
				<a href="/en/coursework/CS2301/" title="Operating Systems 1">CS 2301</a> |
				<span class="hyperlink_unavailable" title="Software Engineering 1">CS 2401</span> |
				<span class="hyperlink_unavailable" title="Data Structures">CS 3303</span> |
				<span class="hyperlink_unavailable" title="Analysis of Algorithms">CS 3304</span> |
				<span class="hyperlink_unavailable" title="Web Programming 2">CS 3305</span> |
				<span class="hyperlink_unavailable" title="Databases 2">CS 3306</span> |
				<span class="hyperlink_unavailable" title="Operating Systems 2">CS 3307</span> |
				<span class="hyperlink_unavailable" title="Information Retrieval">CS 3308</span> |
				<span class="hyperlink_unavailable" title="Comparative Programming Languages">CS 4402</span> |
				<span class="hyperlink_unavailable" title="Software Engineering 2">CS 4403</span> |
				<span class="hyperlink_unavailable" title="Advanced Networking and Data Security">CS 4404</span> |
				<span class="hyperlink_unavailable" title="Mobile Applications">CS 4405</span> |
				<span class="hyperlink_unavailable" title="Computer Graphics">CS 4406</span> |
				<span class="hyperlink_unavailable" title="Data Mining and Machine Learning">CS 4407</span> |
				<span class="hyperlink_unavailable" title="Artificial Intelligence">CS 4408</span> |
				<span class="hyperlink_unavailable" title="English Composition 2">ENGL 1102</span> |
				<span class="hyperlink_unavailable" title="World Literature">ENGL 1405</span> |
				<span class="hyperlink_unavailable" title="Introduction to Environmental Science">ENVS 1301</span> |
				<a href="/en/coursework/HIST1421/" title="Greek and Roman Civilization">HIST 1421</a> |
				<span class="hyperlink_unavailable" title="College Algebra">MATH 1201</span> |
				<span class="hyperlink_unavailable" title="Calculus">MATH 1211</span> |
				<span class="hyperlink_unavailable" title="Introduction to Statistics">MATH 1280</span> |
				<span class="hyperlink_unavailable" title="Discrete Mathematics">MATH 1302</span> |
				<span class="hyperlink_unavailable" title="Introduction to Philosophy">PHIL 1402</span> |
				<a href="/en/coursework/PHIL1404/" title="Ethics and Social Responsibility">PHIL 1404</a> |
				<a href="/en/coursework/POLS1503/" title="Globalization">POLS 1503</a> |
				<span class="hyperlink_unavailable" title="Introduction to Psychology">PSYC 1504</span> |
				<a href="/en/coursework/UNIV1001/" title="Online Education Strategies">UNIV 1001</a>
			</p>
			<hr/>
			<p>
				<a href="/en/coursework/CS1101/t_area.py.xhtml" title="t_area.py">Unit 2</a> |
				<a href="/en/coursework/CS1101/calculator_flowchart.xhtml" title="calculator flowchart">Unit 3</a> |
				<a href="/en/coursework/CS1101/tryme3.py.xhtml" title="tryme3.py">Unit 4</a> |
				<a href="/en/coursework/CS1101/mycalc.py.xhtml" title="mycalc.py">Unit 5</a> |
				<a href="/en/coursework/CS1101/bool.py.xhtml" title="bool.py">Unit 6</a> |
				<a href="/en/coursework/CS1101/sort_fruits.py.xhtml" title="sort_fruits.py">Unit 7</a>
			</p>
			<hr/>
		</nav>
		<header>
			<h1>bool.py</h1>
			<p>Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalized on 2017-03-08</p>
		</header>
<h2>Flowchart</h2>
<img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/bool.py.png" alt="A flowchart depicting the flow of the script below" width="625" height="1284" />
<h2>Script:</h2>
<blockquote>
<pre><code># Every good program should begin with a license comment that declares the file
# as being covered by a free software license.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.

# This makes Python2 behave like Python3 in regards to user input.
if &quot;raw_input&quot; in vars(__builtins__):
	input = raw_input

# This function compares two numbers and returns what is basically the three-way
# equivalent of a boolean, but as an integer.
def compare(a, b):
	if(a &gt; b):
		return 1
	elif(a == b):
		return 0
# If a isn&apos;t greater than b and a isn&apos;t equal to b, a must be less than b.
#There&apos;s no reason to even check to see if a is less than b.
	else:
		return -1

# User input is in string form, not number form. We need to not only make sure
# the user input a number, but also convert the string representation of that
# number into a numeric representation.
def sanitize_and_parse(string):
	try:
# If the input can be treated as an integer, we&apos;ll do that.
		return int(string)
	except:
		try:
# If the input cannot be treated as an integer but can be treated as a float,
# we&apos;ll do that instead.
			return float(string)
		except:
# If the input isn&apos;t numeric, we&apos;ll simply treat it as zero.
			return 0

# This is a dummy function. We&apos;ll use it if we&apos;re in string mode.
def return_argument(arg):
	return arg

handle_input = None

# This lets us toggle between string mode and numeric mode.
while not handle_input:
	i = input(&apos;Enter &quot;0&quot; or &quot;s&quot; to compare strings.\nEnter &quot;1&quot; or &quot;n&quot; to compare numbers.\n&gt;&gt;&gt; &apos;)
	if i == &quot;0&quot; or i == &quot;s&quot;:
		handle_input = return_argument
		input_type = &quot;string&quot;
	elif i == &quot;1&quot; or i == &quot;n&quot;:
		handle_input = sanitize_and_parse
		input_type = &quot;number&quot;
	else:
		print(&quot;Invalid selection; please try again.&quot;)

# We need to run the function three times.
iterations = 3

# Zero evaluates as False. &quot;while iterations&quot; will work as expected, we don&apos;t
# need &quot;while iterations != 0&quot;.
while iterations:
	a = input(&quot;Please enter the first &quot;+input_type+&quot;.\n&gt;&gt;&gt; &quot;)
	a = handle_input(a)
	b = input(&quot;Please enter the second &quot;+input_type+&quot;.\n&gt;&gt;&gt; &quot;)
	b = handle_input(b)
# In Python2, the &quot;print&quot; key word won&apos;t output the correct message if we use
# multiple arguments to print() without removing the parentheses. However,
# removing the parentheses causes a syntax error in Python3. The only option
# that seems to work in both versions of the language is to concatenate the two
# things we need to print. We can&apos;t concatenate though without casting to string
# first, so we also have to do that.
	print(&quot;Return code: &quot;+str(compare(a, b)))
	iterations -= 1</code></pre>
</blockquote>
<h2>Output:</h2>
<blockquote>
<pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.
&gt;&gt;&gt; ================================ RESTART ================================
&gt;&gt;&gt; 
Enter &quot;0&quot; or &quot;s&quot; to compare strings.
Enter &quot;1&quot; or &quot;n&quot; to compare numbers.
&gt;&gt;&gt; n
Please enter the first number.
&gt;&gt;&gt; 5
Please enter the second number.
&gt;&gt;&gt; 2
Return code: 1
Please enter the first number.
&gt;&gt;&gt; 2
Please enter the second number.
&gt;&gt;&gt; 5
Return code: -1
Please enter the first number.
&gt;&gt;&gt; 3
Please enter the second number.
&gt;&gt;&gt; 3
Return code: 0
&gt;&gt;&gt;</pre>
</blockquote>
		<hr/>
		<p>
			Copyright © 2017 Alex Yst;
			You may modify and/or redistribute this document under the terms of the <a rel="license" href="/license/gpl-3.0-standalone.xhtml"><abbr title="GNU&apos;s Not Unix">GNU</abbr> <abbr title="General Public License version Three or later">GPLv3+</abbr></a>.
			If for some reason you would prefer to modify and/or distribute this document under other free copyleft terms, please ask me via email.
			My address is in the source comments near the top of this document.
			This license also applies to embedded content such as images.
			For more information on that, see <a href="/en/a/licensing.xhtml">licensing</a>.
		</p>
		<p>
			<abbr title="World Wide Web Consortium">W3C</abbr> standards are important.
			This document conforms to the <a href="https://validator.w3.org./nu/?doc=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1101%2Fbool.py.xhtml"><abbr title="Extensible Hypertext Markup Language">XHTML</abbr> 5.1</a> specification and uses style sheets that conform to the <a href="http://jigsaw.w3.org./css-validator/validator?uri=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1101%2Fbool.py.xhtml"><abbr title="Cascading Style Sheets">CSS</abbr>3</a> specification.
		</p>
	</body>
</html>

